/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-12-27 15:16:05
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2024-04-15 11:26:24
 */
import { Controller, Get, Post, Req, Res,Session } from '@nestjs/common';
import {  Response } from 'express';
import xlsx from 'node-xlsx';
import fs from 'fs';
import path from 'path';
import {ExcelService} from './excel.service'
import { Uint8ArrayToString } from '../../utils/datasource'
import { UsersService} from '../../users/users.service'
@Controller('excel')
export class ExcelController {
   constructor(private readonly excelService: ExcelService,private userRepository: UsersService) {}

   @Get('/export-excel-user')
   async exportExcel(@Res() res: Response): Promise<void> {

    const users =  await this.userRepository.findAll({
          skip:0,
          take:99999,
          keyword:'',
    })

    //  console.log('输出用户',users)
     let list =  users.list.map((element,index) => {
       return {
        ['序号']:index+1,
        id: element.id,
        ['电话']: element.phone,
        ['用户名']: element.username,
       }
    });

    const fileName = 'users.xlsx';
    const buffer = await this.excelService.exportExcel(list, fileName);
    res.set({
      'Content-Disposition': `attachment; filename=${fileName}`,
      'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    });
    const string = Uint8ArrayToString(buffer)
    res.send(Buffer.from(string, 'binary'));
  }
}
